/**
  ******************************************************************************
  * @file    event.h
  * @author  Dragon.Chen
  * @version V1.0.0
  * @date    12-25-2014
  * @brief   事件处理
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 上海今中网络科技有限公司 </center></h2>
  ******************************************************************************
  */

#ifndef __EVENT__H__
#define __EVENT__H__

#include "pool.h"
#include "timer.h"
#include "sqlitex.h"
#include "ssl-util.h"
#include "otherconfig.h"
#include "socket-util.h"
#include "charconvert.h"
#include "log-handle.h"
#include "filter.h"


extern int                     *VPNPortCount;
extern sqlite3                 *dbHandle;
///****************patched by yagamishi add*************2016/02/18*******
///******************发送日志********************************************
extern sqlite3                 *ZDLogDbHandle;
///*************************************************************************
extern X509_CRL                *CertRevokedList;
extern X509_STORE              *RootCertStore;
extern SOCK_BUFFER_MANAGE_T     SockBufferManage;
extern CERTIFICATE_MANAGE_T     CertificateManage;
extern CLIENT_DATA_MANAGE_T     ClientDataManage;

///****************patched by xiaohu mod*************2016/03/18*******
extern int                      PreAndroidTypeDoor;
///*************************************************************************

#define VPN_RULE_NULL           "RULE_NULL"

/// 添加用户信息到数据库
//****************patched by yagamishi add*************2016/02/04*******
//******************添加android类型显示,多添加了loginBind->androidType，reservedItem3*****************************
#define InsertUserInformation() \
            ZeroMemory(sqlBuffer, SQL_BUFLEN); \
            snprintf(sqlBuffer, SQL_BUFLEN, \
                 "INSERT INTO MPSInfo(serialNumber, userName, userNameAbbreviation, province, city, " \
                 "organization, institution, emailAddress, hardwarePlatform, hardwareSn, " \
                 "OSVersion, phoneNumber, clientVersion, reservedItem3) VALUES (\'%s\', \'%s\', \'%s\', \'%s\', " \
                 "\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\' )", \
                 loginBind->serialNumber, loginBind->commomName, inBuffer, loginBind->province, \
                 loginBind->city,         loginBind->organization,         loginBind->institution, \
                 loginBind->email,        loginBind->hardwarePlatform,     loginBind->hardwareSN, \
                 loginBind->OSVersion,    loginBind->phoneNumber,          loginBind->clientVersion, \
                 loginBind->androidType); \
\
            if (InsertDataIntoDatabase(dbHandle, sqlBuffer, errmsg) != SQLITE_OK) \
            { \
                MSG(LOG_ERROR, "[handle_login_constraint:import_user_info] %s!", errmsg); \
                sqlite3_free(errmsg); \
                goto finish; \
            } \
//*********************************************************************

/// 更新数据库
//****************patched by yagamishi add*************2016/02/04*******
//******************添加android类型显示,多添加了loginBind->androidType，reservedItem3*****************************
#define UpdateUserInformation() \
            ZeroMemory(sqlBuffer, SQL_BUFLEN); \
            snprintf(sqlBuffer,  SQL_BUFLEN, "UPDATE MPSInfo SET hardwarePlatform = \'%s\', hardwareSn = \'%s\', " \
                 "OSVersion = \'%s\', phoneNumber = \'%s\', clientVersion = \'%s\',  reservedItem3 = \'%s\' " \
                 "WHERE userName = \'%s\'", \
                 loginBind->hardwarePlatform, loginBind->hardwareSN,    loginBind->OSVersion, \
                 loginBind->phoneNumber,      loginBind->clientVersion,  loginBind->androidType, loginBind->commomName); \
\
            if (UpdateDataFromDatabase(dbHandle, sqlBuffer, errmsg) != SQLITE_OK) \
            { \
                MSG(LOG_ERROR, "[handle_login_constraint:update_user_info] %s!", errmsg); \
                sqlite3_free(errmsg); \
                goto finish; \
            } \
//*********************************************************************
/// 设置默认规则
#define GenerateDefaultRules() \
            ZeroMemory(sqlBuffer, SQL_BUFLEN); \
            snprintf(sqlBuffer, SQL_BUFLEN,  "INSERT INTO VpnuserRules(rulesSN, rulesCN, rulesHardwarePlatform, " \
                 "rulesHardwareSn, rulesOSVersion, rulesPhoneNumber, rulesClientVersion, " \
                 "rulesTestCount, rulesDoVerify) " \
                 "VALUES (\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%c\', \'%c\')",  \
                 RuleConfig.serialNumber     ? loginBind->serialNumber      : "", \
                 RuleConfig.commomName       ? loginBind->commomName        : "", \
                 RuleConfig.hardwarePlatform ? loginBind->hardwarePlatform  : "", \
                 RuleConfig.hardwareSN       ? loginBind->hardwareSN        : "", \
                 RuleConfig.OSVersion        ? loginBind->OSVersion         : "", \
                 RuleConfig.phoneNumber      ? loginBind->phoneNumber       : "", \
                 RuleConfig.clientVersion    ? loginBind->clientVersion     : "", \
                 '0' + RuleConfig.nLogin, \
                 '0' + RuleConfig.verify); \
\
            if (InsertDataIntoDatabase(dbHandle, sqlBuffer, errmsg) != SQLITE_OK) \
            { \
                MSG(LOG_ERROR, "[handle_login_constraint:import_rule_info] %s!", errmsg); \
                sqlite3_free(errmsg); \
                goto finish; \
            } \

/// 验证指定字段是否合法
#define VerifySpecifiedField()  \
            ((result[6]  == NULL || strlen(result[6])  == 0) ? 1 : streql(result[6],  loginBind->serialNumber))     && \
            ((result[7]  == NULL || strlen(result[7])  == 0) ? 1 : streql(result[7],  loginBind->hardwarePlatform)) && \
            ((result[8]  == NULL || strlen(result[8])  == 0) ? 1 : streql(result[8],  loginBind->hardwareSN))       && \
            ((result[9]  == NULL || strlen(result[9])  == 0) ? 1 : streql(result[9],  loginBind->OSVersion))        && \
            ((result[10] == NULL || strlen(result[10]) == 0) ? 1 : streql(result[10], loginBind->phoneNumber))      && \
            ((result[11] == NULL || strlen(result[11]) == 0) ? 1 : streql(result[11], loginBind->clientVersion))


/******************************* 处理请求 *******************************/
/**
  * @brief  认证版本号
  * @param  arg1:用户数据   arg2:请求数据
  * @retval RETURN_SUCCESS
  */
int handle_version(CLIENT_DATA_T *clientData, char *msg);

///****************patched by yagamishi add*************2016/02/18*******
///******************发送日志********************************************
/**
  * @brief  终端加固日志写入数据库并按要求是否把用户T下线
  * @param  arg1:用户数据  arg2:请求数据
  * @retval RETURN_SUCCESS
  */
int zdjg_write_to_db(CLIENT_DATA_T *clientData, char *msg);
///**********************************************************************

/**
  * @brief  认证证书是否合法
  * @param  arg1:用户数据
  * @retval 成功返回RETURN_SUCCESS，否则RETURN_FAILURE
  */
int verify_certificate(CLIENT_DATA_T *clientData);

/**
  * @brief  认证证书是否过期
  * @param  arg1:用户数据  arg2:请求数据
  * @retval 成功返回RETURN_SUCCESS，否则RETURN_FAILURE
  */
int verify_certificate_revoke_list(CLIENT_DATA_T *clientData, char *msg);

/**
  * @brief  检查用户绑定约束
  * @param  arg1:用户数据  arg2:请求数据
  * @retval 成功返回RETURN_SUCCESS，否则RETURN_FAILURE
  */
int verify_bind_constraint(CLIENT_DATA_T *clientData, char *msg, char pmos);

//****************patched by yagamishi add*************2015/8/17*******
/**
  * @brief  启动iptables，登陆成功后的用户所有
  * @param  arg1:用户数据
  * @retval NULL
  */
void generate_iptables(CLIENT_DATA_T *clientData);
//*********************************************************************


/**
  * @brief  生成openvpn配置文件
  * @param  arg1:用户数据  arg2:请求数据
  * @retval 成功返回RETURN_SUCCESS，否则RETURN_FAILURE
  */
int generate_openvpn_config(CLIENT_DATA_T *clientData, char *msg);

/**
  * @brief  生成stunnel配置文件
  * @param  arg1:用户数据  arg2:请求数据
  * @retval 成功返回RETURN_SUCCESS，否则RETURN_FAILURE
  */
int generate_stunnel_config(CLIENT_DATA_T *clientData, char *msg);

/**
  * @brief  生成utunnel配置文件
  * @param  arg1:用户数据  arg2:请求数据
  * @retval 成功返回RETURN_SUCCESS，否则RETURN_FAILURE
  */
int generate_utunnel_config(CLIENT_DATA_T *clientData, char *msg);

//****************patched by yagamishi add*************2016/01/28*******
//******************按进程判断是否启动*****************************
/**
  * @brief  生成process配置文件
  * @param  arg1:用户数据  arg2:请求数据
  * @retval 成功返回RETURN_SUCCESS，否则RETURN_FAILURE
  */
int generate_process_config(CLIENT_DATA_T *clientData, char *msg);
//*********************************************************************
//****************patched by yagamishi add*************2016/02/03*******
//******************按手机型号判断是否启动*****************************
/**
  * @brief  生成android_type配置文件
  * @param  arg1:用户数据  arg2:请求数据
  * @retval 成功返回RETURN_SUCCESS，否则RETURN_FAILURE
  */
int generate_android_type_config(CLIENT_DATA_T *clientData, char *msg);
//*********************************************************************

//****************patched by yagamishi add*************2016/02/10*******
//******************检查正在运行的东西是否符合规则*****************************
/**
  * @brief  生成checkrunning配置文件
  * @param  arg1:用户数据  arg2:请求数据
  * @retval 成功返回RETURN_SUCCESS，否则RETURN_FAILURE
  */
//int generate_check_running_config(CLIENT_DATA_T *clientData, char *msg);
//*********************************************************************

///****************patched by yagamishi add*************2016/02/17*******
///******************检查正在运行的东西是否符合规则*****************************
/**
  * @brief  得到checkrunningswitch.conf内容格式化
  * @param  arg1:用户数据  arg2:请求数据
  * @retval 成功返回RETURN_SUCCESS，否则RETURN_FAILURE
  */
int generate_check_running_switch_config(CLIENT_DATA_T *clientData, char *msg);
int generate_android_check_switch_config(CLIENT_DATA_T *clientData, char *msg);
int generate_android_check_running_switch_config(CLIENT_DATA_T *clientData, char *msg);
int generate_android_type_door_config(CLIENT_DATA_T *clientData, char *msg);
///*********************************************************************

#endif

/******************* (C) COPYRIGHT 2014 上海今中网络科技有限公司 *****END OF FILE****/






